package com.yuchuang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuchuang.base.PageInfo;
import com.yuchuang.mapper.UserMapper;
import com.yuchuang.model.User;
import com.yuchuang.model.vo.UserVo;
import com.yuchuang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User getUser(Map<String,Object> params){
        User user = userMapper.getUserImlBatis(params);
        return user;
    }
    @Override
    public List<User> queryAll(HttpServletRequest request){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        String str = request.getParameter("userNickname");
        queryWrapper.like(request.getParameter("userNickname") != null,"user_nickname",request.getParameter("userNickname"));
        List<User> list = userMapper.queryAllImplBatis(queryWrapper);
        return list;
    }
    @Override
    public int insert(User user){
        int val = userMapper.insertImplBatis(user);
        return val;
    }
    @Override
    public int update(Map<String,Object> params){
        int val = userMapper.updateImplBatis(params);
        return val;
    }
    @Override
    public int delete(Map<String,Object> params){
        int val = userMapper.deleteImplBatis(params);
        return val;
    }
    @Override
      public PageInfo selectPageVo(UserVo userVo){
        Page<User> page = new Page<>(userVo.getPageNum(),userVo.getPageSize());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(userVo.getUserNickname() != null,"user_nickname",userVo.getUserNickname());
         IPage<User> result = userMapper.selectPageVoMapper(page,queryWrapper);
            int pageNums =(int) result.getPages();
            int size =(int) result.getSize();
            int pageNum =(int) result.getCurrent();
            int total =(int) result.getTotal();
            List t = result.getRecords();
        return new PageInfo().setContent(t).setPage(pageNum).setSize(size).setTotal(total).setPages(pageNums);
    }
}
